/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.widgets.visual.results;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.jgrapht.DirectedGraph;
import org.jgrapht.graph.DefaultDirectedGraph;

import dgbgui.gui.widgets.Widget;
import dgbgui.gui.widgets.WidgetFactory;
import dgbgui.gui.widgets.layouts.GraphLayout;
import dgbgui.gui.widgets.layouts.HierarchicalTreeLayout;
import dgbgui.gui.widgets.visual.EdgeWidget;
import dgbgui.modeldocument.primitives.DefaultAnnotatedEdge;
import dgbgui.modeldocument.primitives.DefaultEdgeWithWidget;
import dgbgui.modeldocument.primitives.Graph;
import dgbgui.modeldocument.primitives.Primitive;
import edu.umd.cs.piccolo.PNode;

@SuppressWarnings("serial")
public class GraphResultWidget extends ResultWidget {
    
    public GraphResultWidget(PNode parent, Graph primitiveGraph) {
        super(parent,primitiveGraph);
        
        m_layout = new HierarchicalTreeLayout(this, 5.0, 30.0);
        
        DirectedGraph<Widget,DefaultEdgeWithWidget> widgetGraph = 
            new DefaultDirectedGraph<Widget,DefaultEdgeWithWidget>(DefaultEdgeWithWidget.class);
        
        // Temporary mapping between primitives and visula representation
        Map<Primitive,ResultWidget> toVisualMap = new HashMap<Primitive,ResultWidget>();
        
        // Create a widget for each vertex, add it to the widget 
        // graph and also the temporary map
        for ( Primitive vertex : primitiveGraph.vertexSet() ) {
            ResultWidget vertexWidget = WidgetFactory.createWidget(m_layout,vertex);
            widgetGraph.addVertex(vertexWidget);
            toVisualMap.put(vertex,vertexWidget);

        }
        
        // Create the edges
        for ( DefaultAnnotatedEdge edge : primitiveGraph.edgeSet() ) {
            
            Primitive sourcePrimitive = primitiveGraph.getEdgeSource(edge);
            Primitive targetPrimitive = primitiveGraph.getEdgeTarget(edge);
            
            EdgeWidget edgeWidget = new EdgeWidget(
                    m_layout,
                    "");//edge.getAnnotation());

            widgetGraph.addEdge(
                    toVisualMap.get(sourcePrimitive), 
                    toVisualMap.get(targetPrimitive),
                    new DefaultEdgeWithWidget(edgeWidget));
        }
        
        ((HierarchicalTreeLayout)m_layout).setGraph(widgetGraph);
    }
    
    GraphLayout m_layout = null;
    
    public Set<Widget> getVertexWidgets() {
        return m_layout.vertexSet();
    }

}
